package livesun.justiceeternal.fragment;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import java.lang.reflect.Field;
import livesun.justiceeternal.R;
import livesun.justiceeternal.activity.ScoreInforActivity;
import livesun.justiceeternal.adapter.ItemsBean;
import livesun.justiceeternal.adapter.ScoreAdapter;
import livesun.justiceeternal.adapter.ScoreBean;
import livesun.justiceeternal.base.BaseFragment;
import livesun.justiceeternal.base.Contract;
import livesun.justiceeternal.contract.IGetScoreContract;
import livesun.justiceeternal.persenter.GetScorePersenter;
import zlc.season.practicalrecyclerview.PracticalRecyclerView;

/**
 * Created by 29028 on 2017/8/10.
 */

public class GetScoreFragment extends BaseFragment implements  PracticalRecyclerView.OnRefreshListener,
        PracticalRecyclerView.OnLoadMoreListener,IGetScoreContract.View,ScoreAdapter.OnItemClickListener{

    private PracticalRecyclerView recycleView;
    private ScoreAdapter mAdapter;
    private GetScorePersenter scorePersenter;
    private String keyWords="";
    private Intent mIntent;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public int getResourcesId() {
        mIntent = new Intent(getActivity(), ScoreInforActivity.class);
        return R.layout.score_fragment;
    }

    @Override
    public void initView() {
        scorePersenter = new GetScorePersenter(this,getActivity());
        recycleView = findView(R.id.recycler);
        recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new ScoreAdapter(getActivity());
        recycleView.setAdapterWithLoading(mAdapter);

    }

    @Override
    public void initListener() {
        mAdapter.setOnItemClickListener(this);
        recycleView.setRefreshListener(this);
        recycleView.setLoadMoreListener(this);
        scorePersenter.start();
        try {
            Class aClass = recycleView.getClass();
            Field scrool = aClass.getDeclaredField("mSwipeRefreshLayout");
            scrool.setAccessible(true);
            mSwipeRefreshLayout = (SwipeRefreshLayout) scrool.get(recycleView);
            mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
            mSwipeRefreshLayout.measure(0,0);
            mSwipeRefreshLayout.setRefreshing(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onLoadMore() {
        scorePersenter.loardMore();
    }

    @Override
    public void onRefresh() {
        scorePersenter.start();
    }


    @Override
    public void getDatasSucess(ScoreBean itemsBean) {
        mAdapter.clear();
        mAdapter.addAll(itemsBean.getItems());
    }

    @Override
    public void getDataError(String erro) {
        mAdapter.showError();
    }

    @Override
    public void loadMoreSucess(ScoreBean itemsBean) {
        mAdapter.addAll(itemsBean.getItems());
    }

    @Override
    public void loardMoreError(String erro) {
        mAdapter.loadMoreFailed();
    }

    @Override
    public String getKeyWords() {
        return keyWords;
    }


    @Override
    public void setPersenter(IGetScoreContract.Persenter persenter) {
        this.scorePersenter = (GetScorePersenter) persenter;
    }

    @Override
    public void itemClick(int position, ItemsBean scoreBean) {
        mIntent.putExtra(Contract.ITEMBEAN,scoreBean);
        startActivity(mIntent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(scorePersenter!=null){
            scorePersenter.cancel();
        }
    }
}
